﻿using System;
using System.Activities;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using Common.Activities.Design;
using Microsoft.Practices.Prism;
using Microsoft.Practices.Prism.Regions;
using Microsoft.Practices.ServiceLocation;

namespace Common.Activities
{
    [Designer(typeof(ClearViewsFromRegionDesigner))]
    public class ClearViewsFromRegion : NativeActivity
    {
        [RequiredArgument]
        public InArgument<string> RegionName { get; set; }

        protected override void Execute(NativeActivityContext context)
        {
            var regionNameValue = context.GetValue(this.RegionName);

            Helper.MarshalToUiThread(delegate
                                         {
                                             var regionManager = ServiceLocator.Current.TryResolve<IRegionManager>();
                                             List<object> views =
                                                 new List<object>(regionManager.Regions[regionNameValue].Views);
                                             foreach (object view in views)
                                             {
                                                 regionManager.Regions[regionNameValue].Remove(view);
                                             }
                                         });

        }
    }
}
